package com.cserver.pro.service.impl;


import com.cserver.pro.dao.SysUserDao;
import com.cserver.pro.entity.SysUser;
import com.cserver.pro.entity.YqBaseinfo;
import com.cserver.pro.service.SysUserService;
import com.cserver.pro.utils.SerializeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * 用户信息表(SysUser)表服务实现类
 *
 * @author makejava
 * @since 2020-02-22 09:45:22
 */
@Service("sysUserService")
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    private SysUserDao sysUserDao;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 通过ID查询单条数据
     *
     * @param userId 主键
     * @return 实例对象
     */
    @Override
    public SysUser queryById(Long userId) {
        return this.sysUserDao.queryById(userId);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<SysUser> queryAllByLimit(int offset, int limit) {

        return this.sysUserDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param sysUser 实例对象
     * @return 实例对象
     */
    @Override
    public SysUser insert(SysUser sysUser) {
        this.sysUserDao.insert(sysUser);
        return sysUser;
    }

    /**
     * 修改数据
     *
     * @param sysUser 实例对象
     * @return 实例对象
     */
    @Override
    public SysUser update(SysUser sysUser) {
        this.sysUserDao.update(sysUser);
        return this.queryById(sysUser.getUserId());
    }

    /**
     * 通过主键删除数据
     *
     * @param userId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long userId) {
        return this.sysUserDao.deleteById(userId) > 0;
    }

    @Override
    public SysUser getSysUser(String loginName, String password) {


        SysUser sysUser=null;
        if (redisTemplate.hasKey("sysUser")){
            System.out.println("缓存");
            sysUser=(SysUser)redisTemplate.opsForValue().get("sysUser");
        }else{
            System.out.println("数据库");
            sysUser=this.sysUserDao.getSysUser(loginName,password);
            redisTemplate.opsForValue().set("sysUser",sysUser);
            redisTemplate.expire("sysUser",20, TimeUnit.SECONDS);
        }

//        return this.sysUserDao.getSysUser(loginName,password);
        return sysUser;
    }
}